<template>
  <!-- <swiper
    :modules="modules"
    :loop="false"
    :slides-per-view="3"
    :space-between="35"
    :navigation="true"
  >
    <swiper-slide v-for="(item, index) in imageList" :key="item._id">
      <img
        :src="item.response.url"
        :class="{ active: currentIndex == index }"
        @click="changeCurrentIndex(index)"
      />
    </swiper-slide>
  </swiper> -->
  <div class="swiper" ref="cur">
    <div class="swiper-wrapper">
      <div
        class="swiper-slide"
        v-for="(item, index) in imageList"
        :key="item._id"
      >
        <img
          :src="item.response.url"
          :class="{ active: currentIndex == index }"
          @click="changeCurrentIndex(index)"
        />
      </div>
    </div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script setup lang="ts" name="imageList">
import { ref, watch, onMounted, nextTick } from "vue";
import { Swiper } from "swiper";
// @ts-ignore
import { SwiperOptions } from "swiper/types";
import EventBus from "@/utils/bus";

import "swiper/css";
import "swiper/css/navigation";

const props = defineProps(["imageList"]);

let currentIndex = ref(0);
const swiperParams: SwiperOptions = {
  slidesPerView: 3,
  spaceBetween: 35,
};
const swiper = new Swiper(".swiper", swiperParams);

const changeCurrentIndex = (index: number) => {
  // 修改响应式数据
  currentIndex.value = index;
  EventBus.emit("getIndex", currentIndex.value);
};
watch(props.imageList, () => {
  nextTick(() => {
    const swiperParams: SwiperOptions = {
      slidesPerView: 3,
      spaceBetween: 35,
    };
    const swiper = new Swiper(".swiper", swiperParams);
    swiper.init()
  });
});
onMounted(() => {
  swiper.init();
});
</script>

<style lang="scss">
.swiper {
  width: 450px;
  box-sizing: border-box;
  padding: 0 12px;
  margin-top: 10px;

  .swiper-slide {
    width: 56px;
    height: 56px;

    img {
      width: 100%;
      height: 100%;
      border: 1px solid #ccc;
      width: 53px;
      height: 53px;
      display: block;
      &.active {
        border: 2px solid #f41509;
      }
    }
  }

  .swiper-button-next {
    left: auto;
    right: 0;
  }

  .swiper-button-prev {
    left: 0;
    right: auto;
  }

  .swiper-button-next,
  .swiper-button-prev {
    box-sizing: border-box;
    width: 12px;
    height: 56px;
    color: #000;
    background: rgb(235, 235, 235);
    border: 1px solid rgb(204, 204, 204);
    top: 0;
    margin-top: 0;
    &::after {
      font-size: 16px;
    }
  }
}
</style>
